/*
 ac_test.cpp - Aho-Corasick test program.

 Create ac.dat file like below. One word per line.
 
 Hi
 Hello
 rope
 rar
 
 Build and execute ac_test, input a test string, for example, "Hello, It's rope library test program."
 then Aho-Corasick algorithm matches patterns in the test string and replace it.
 
 output : "[CENSORED], It's [CENSORED] lib[CENSORED]y test program."
*/
#include "../rope/aho_corasick.h"

#include <iostream>
#include <fstream>

typedef aho_corasick<std::string> ac;

int main()
{
	std::ifstream datafile("./ac.dat");
	if (!datafile) {
		std::cerr << "No ./ac.dat file" << std::endl;
		return EXIT_FAILURE;
	}

	ac::pattern_list patterns;
	std::string line;
	for (; std::getline(datafile, line);) {
		patterns.push_back(line);
	}
	std::cout << "pattern file loading done" << std::endl;

	try
	{
		aho_corasick<> ac(patterns);

		std::string pattern("test_pattern");
		ac.add(pattern.begin(), pattern.end());
		ac.build();

		std::cout << "building done" << std::endl;
		while(std::getline(std::cin, line))
		{
			ac::result_list_t result = ac.search(line);
			std::cout << ac.replace(line, "[CENSORED]", result) << std::endl;
			std::cout << ac.replace_bf(line) << std::endl;;
		}
	}
	catch (const std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

	return 0;
}
